package com.ouyang.dao.oders;

import com.ouyang.dao.DataBaseDao;
import com.ouyang.poji.Oders;
import com.ouyang.poji.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OdersDaoImpl implements OdersDao {


    @Override
    public List<Oders> getlist ( ) {
        Connection connection = DataBaseDao.getConnection();
        PreparedStatement statement = null;
        String sql = "select * from oders";
        ResultSet rs = null;
        List<Oders> list = new ArrayList<>();
        try {
            statement = connection.prepareStatement(sql);
            rs = statement.executeQuery();
            while (rs.next()){
                Oders oders = new Oders();
                oders.setId(rs.getInt(1));
                oders.setOrderNumber(rs.getString(2));
                oders.setCdbName(rs.getString(3));
                oders.setRentPeople(rs.getString(4));
                list.add(oders);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataBaseDao.closeResource(connection,statement,rs);
        }
        return list;
    }

    @Override
    public Integer addoders ( String OrderNumber, String cdbName, String RentPeople ) {
        Connection connection = DataBaseDao.getConnection();
        PreparedStatement statement = null;
        String sql = "insert into oders(OrderNumber,cdbName,RentPeople) values(?,?,?)";
        Integer rs = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1,OrderNumber);
            statement.setString(2,cdbName);
            statement.setString(3,RentPeople);
            rs = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataBaseDao.closeResource(connection,statement,null);
        }
        return rs;
    }

    @Override
    public Integer deloders ( Integer id ) {
        Connection connection = DataBaseDao.getConnection();
        PreparedStatement statement = null;
        String sql = "delete from oders where Id = "+id;
        Integer rs = null;
        try {
            statement = connection.prepareStatement(sql);
            rs = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataBaseDao.closeResource(connection,statement,null);
        }
        return rs;
    }

    @Override
    public Integer update ( Integer id, String ordernumber, String cdbname, String rentpeople ) {
        Connection connection = DataBaseDao.getConnection();
        PreparedStatement statement = null;
        String sql = "update oders set OrderNumber = ?,cdbName = ?,RentPeople = ? where id = ?";
        Integer rs = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1,ordernumber);
            statement.setString(2,cdbname);
            statement.setString(3,rentpeople);
            statement.setString(4, String.valueOf(id));
            rs = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataBaseDao.closeResource(connection,statement,null);
        }
        return rs;
    }

    @Override
    public List<Oders> search ( String key, String value ) {
        Connection connection = DataBaseDao.getConnection();
        PreparedStatement statement = null;
        String sql = "select * from oders where "+key+" like '%"+value+"%'";
        ResultSet rs = null;
        List<Oders> list = new ArrayList<>();
        try {
            statement = connection.prepareStatement(sql);
            rs = statement.executeQuery();
            while (rs.next()){
                Oders oders = new Oders();
                oders.setId(rs.getInt(1));
                oders.setOrderNumber(rs.getString(2));
                oders.setCdbName(rs.getString(3));
                oders.setRentPeople(rs.getString(4));
                list.add(oders);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataBaseDao.closeResource(connection,statement,rs);
        }
        return list;
    }
}
